// pages/contact/joinus2.js
const app = getApp()
const nt_util = require('../../utils/nt_util.js')
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');

// 实例化腾讯地图API核心类
var qqmapsdk = new QQMapWX({
  key: app.globalData.QQMapWXKey // 必填
});
Page({

  /**
   * 页面的初始数据
   */
  data: {
    queryBean:{},
    address: '',
    addressInfo: '',
    location: { lat: '', lng: '' },
    map_width: '',
    map_height: '',
    controls: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    nt_util.that = this;
    var queryBean = JSON.parse(options.queryBean);
    nt_util.that.setData({
      queryBean: queryBean
    }),
    //1、获取当前位置坐标
    wx.getLocation({
      type: 'gcj02',// 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function (res) {
        //2、根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (addressRes) {
            var address = addressRes.result.formatted_addresses.recommend;
            nt_util.that.setData({
              location: { lat: res.latitude, lng: res.longitude },
              address: address
            })
          }
        })
      }
    }),
      // 动态设置map的宽和高
      wx.getSystemInfo({
        success: function (res) {
          nt_util.that.setData({
            map_width: res.windowWidth - 12 * 2 * 2,
            map_height: res.windowWidth / 2 - 10,
            controls: [{
              id: 1,
              iconPath: '/imgs/ic_position.png',
              position: {
                left: res.windowWidth / 2 - 10 - 8 - 15,
                top: res.windowWidth / 4 - 15,
                width: 30,
                height: 30
              },
              clickable: false
            }]
          })
        }
      })
  },
  //获取中间点的经纬度，并mark出来
  getLngLat: function () {
    var that = this;
    this.mapCtx = wx.createMapContext("address_map");
    this.mapCtx.getCenterLocation({
      success: function (res) {
        that.setData({
          location: { lat: res.latitude, lng: res.longitude }
        })
        //根据坐标获取当前位置名称，显示腾讯地图逆地址解析
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (addressRes) {
            if (addressRes) {
              var address = addressRes.result.formatted_addresses.recommend;
              that.setData({
                address: address
              })
            }
          }
        })
      }
    })
  },
  regionchange: function (e) {
    // 地图发生变化的时候，获取中间点，也就是用户选择的位置
    if (e.type == 'end') {
      this.getLngLat()
    }
  },
  bindKeyInput: function (e) {
    var that = this;
    var dataset = e.currentTarget.dataset;
    var data_type = e.detail.value;
    switch (dataset.type) {
      case 'addressInfo':
        that.setData({
          addressInfo: data_type
        });
        break;
    }
  },
  checkParams: function () {
    var that = this;
    if (!that.data.addressInfo) {
      wx.showToast({
        title: '商户地址不能空',
      })
      return false;
    }
    return true;
  },
  tapItem: function (event) {
    if (!this.checkParams()) {
      return false;
    }
    var queryObj = {
      address: this.data.address,
      location: this.data.location,
      addressInfo: this.data.addressInfo,
      queryBean2: this.data.queryBean
    }
    //将对象转为string
    var queryBean = JSON.stringify(queryObj)
    var url = '/pages/contact/joinus3?queryBean=' + queryBean;
    wx.navigateTo({
      url: url
    })
  }, 

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  }
})